package com.byxy.shop.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 为上传文件图片可以在页面上显示
 * ImageController
 */
@Controller
@RequestMapping("static")
public class ImageController {

    @Value("${upload.path}")
	private String path;

	@Autowired
    private ResourceLoader resourceLoader;
    
    @RequestMapping("images")
	public ResponseEntity show(String filename) {
		try {
			// 由于是读取本机的文件，file是一定要加上的， path是在application配置文件中的路径
			return ResponseEntity.ok(resourceLoader.getResource("file:" + path + "/" + filename));
		} catch (Exception e) {
			return ResponseEntity.notFound().build();
		}

	}


}